package com.example.service.impl;

import com.example.mapper.DefectMapper;
import com.example.pojo.Defect;
import com.example.pojo.PageResult;
import com.example.service.DefectService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DefectServiceImpl implements DefectService {

    @Autowired
    private DefectMapper defectMapper;

    //分页查询
    public PageResult page(Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询
        List<Defect> list = defectMapper.findNoMachine();
        Page<Defect> p = (Page<Defect>) list;
        //封装分页结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    public PageResult pagem(Integer page, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询
        List<Defect> list = defectMapper.findMachine();
        Page<Defect> p = (Page<Defect>) list;
        //封装分页结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    //新增
    public void save(Defect defect) {
        defect.setCreateTime(LocalDateTime.now());
        defect.setUpdateTime(LocalDateTime.now());
        defectMapper.insert(defect);
    }

    //删除
    public void deleteById(Integer defectID) {
        defectMapper.deleteById(defectID);
    }

    //修改
    public void update(Defect defect) {
        defect.setUpdateTime(LocalDateTime.now());
        defectMapper.update(defect);
    }

    //获取所有标注后缺陷种类名称
    public Object getAllNames() {
        List<String> list = defectMapper.getAllNames();
        return list;
    }

    //获取所有设备源缺陷种类名称
    public Object getAllMachineNames() {
        List<String> list = defectMapper.getAllMachineNames();
        return list;
    }
}
